package stack;

import java.util.Objects;
import java.util.Stack;

public class Test {

//    static class Student{
//        private Integer stuNum;
//        private String stuName;
//
//        public Student(Integer stuNum, String stuName) {
//            this.stuNum = stuNum;
//            this.stuName = stuName;
//        }
//
//        @Override
//        public boolean equals(Object o) {
//            if (this == o) return true;
//            if (!(o instanceof Student)) return false;
//            Student student = (Student) o;
//            return Objects.equals(stuNum, student.stuNum) &&
//                    Objects.equals(stuName, student.stuName);
//        }
//
//        @Override
//        public int hashCode() {
//            return Objects.hash(stuNum, stuName);
//        }
//    }

    public static void main4(String[] args) {

//       Student student1 = new Student(1001,"zahgnsan");
//       Student student2 = new Student(1001,"zahgnsan");
//        System.out.println(student1.equals(student2));
//        System.out.println(student1 == student2);

    }

    public static void main3(String[] args) {

        String[] tokens = {"10","6","9","3","+","-11","*","/","*","17","+","5","+"};

        System.out.println(evalRPN(tokens));

    }

    public static void main2(String[] args) {

        String str = "(()))))";
        System.out.println(isValid(str));
    }

    public static void main1(String[] args) {

        MyStack stack = new MyStack();

        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        stack.push(5);
        stack.push(6);
        System.out.println(stack.peek());
        stack.pop();
        System.out.println(stack.peek());
        while(!stack.isEmpty()){
            System.out.print(stack.peek()+"  ");
            stack.pop();
        }

        Stack<Integer> stack1 = new Stack<>();
        String s = "qwe";
        System.out.println();
    }

    //逆波兰表达式
    public static int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(String s : tokens){
            if(!isOperation(s)){
                stack.push(Integer.parseInt(s));
            }else{
                int num2 = stack.peek();
                stack.pop();
                int num1 = stack.peek();
                stack.pop();
                switch(s){
                    case "+":
                        stack.push(num1 + num2);
                        break;
                    case "-":
                        stack.push(num1 - num2);
                        break;
                    case "*":
                        stack.push(num1 * num2);
                        break;
                    case "/":
                        stack.push(num1 / num2);
                        break;
                }
            }
        }
        return stack.peek();
    }

    public static boolean isOperation(String s){
        if(s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")){
            return true;
        }
        return false;
    }


    //括号匹配
    public static boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        int i;
        for(i = 0; i < s.length(); i++){
            char ch = s.charAt(i);
            //左括号入栈
            if( '(' == ch || '{' == ch || '[' == ch){
                stack.push(ch);
            }else{
                //右括号
                //栈为空，跳出循环
                if(stack.empty()){
                    break;
                }
                //栈不为空，与栈顶元素匹配
                char top = stack.peek();
                //匹配成功，弹出栈顶元素
                if('(' == top && ')' == ch || '{' == top && '}' == ch || '[' == top && ']' == ch){
                    stack.pop();
                }else{
                    //匹配不成功，直接返回
                    return false;
                }
            }
        }
        //跳出循环的条件有两个
        //1、循环走完
        //2、栈为空
        //3、循环走完，栈为空（匹配完成）
        //无法判断是哪个条件出的循环
        //所以需要判断一下
        //并且当其中一个条件不满足时，此时说明括号匹配失败
        if(i != s.length() || !stack.empty()){
            return false;
        }
        return true;
    }

    public boolean IsPopOrder (int[] pushV, int[] popV) {
        // write code here
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for (int i = 0; i < pushV.length; i++) {
            if (stack.empty()) {
                stack.push(pushV[i]);
            }else{
                if(stack.peek() != popV[j]) {
                    stack.push(pushV[i]);
                }
            }
            while (!stack.empty() && j < popV.length && stack.peek() == popV[j]) {
                stack.pop();
                j++;
            }
        }
        return stack.empty();
    }

}
